package com.snda.sc.hps;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.snda.sc.utils.CommonUtils;
import com.snda.sc.utils.Signs;

public class HPSConnector {

	private HttpClient client;

	public static final String BIND_API = "thirdaccount.AutoBindThirdAccountLogin";
	public static final String BIND_PATH = "apl_thirdaccount4sdg";
	public static final String BIND_ENDPOINT_TYPE = "1";
	public static final String BIND_SIGN_METHOD = "MD5";

	public HPSConnector() {
		PoolingClientConnectionManager connMgr = new PoolingClientConnectionManager();
		connMgr.setMaxTotal(8);

		client = new DefaultHttpClient(connMgr);
		HttpParams params = client.getParams();

		HttpConnectionParams.setConnectionTimeout(params, 600);
		HttpConnectionParams.setSoTimeout(params, 1000);
	}

	public HPSResponse get(SP sp, String companyId, HPSOption option) throws Exception {

		Map<String, String> params = new HashMap<>();
		params.put("method", BIND_API);
		params.put("osap_user", sp.getOsapUser());
		params.put("CompanyId", companyId);
		params.put("AccountId", option.snsUid);
		params.put("AppId", option.appId);
		params.put("AreaId", option.appArea);
		params.put("EndpointIp", option.clientIp);
		params.put("SpId", sp.getSpid());
		params.put("EndpointType", BIND_ENDPOINT_TYPE);
		params.put("CallTime", CommonUtils.formatTime());
		params.put("GUID", CommonUtils.uuid());
		params.put("IsAdult", option.isAdult);
		params.put("signature_method", BIND_SIGN_METHOD);
		params.put("signature", Signs.createSignWithSymbolSecretBehind(params, sp.getSecret()));

		URIBuilder builder = new URIBuilder(sp.getUrl() + BIND_PATH);
		for (Entry<String, String> entry : params.entrySet()) {
			builder.addParameter(entry.getKey(), entry.getValue());
		}

		HttpGet get = new HttpGet(builder.build());
		HttpResponse response = client.execute(get);
		HPSResponse result = new HPSResponse(response.getStatusLine().getStatusCode(), EntityUtils.toString(response
				.getEntity()));

		HttpClientUtils.closeQuietly(response);
		return result;
	}

	public void destroy() {
		HttpClientUtils.closeQuietly(client);
	}

	public static class HPSResponse {
		private int statusCode;
		private String content;

		public HPSResponse(int statusCode, String content) {
			this.statusCode = statusCode;
			this.content = content;
		}

		public int getStatusCode() {
			return statusCode;
		}

		public String getContent() {
			return content;
		}
	}
}
